package workforce.limousine.struts;

import limousine.util.Token;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ActionExceptionHandler extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        /*public ActionForward execute(Exception ex, ExceptionConfig ae,ActionMapping mapping,ActionForm formInstance,
        HttpServletRequest request,HttpServletResponse response)throws ServletException {
         */
        System.out.println("Inside execute of ActionExceptionHandler");

        HttpSession session = request.getSession();

        session.removeAttribute(Token.MSG_ERROR);

        //Exception handler;
        Exception e2 = (Exception) request.getAttribute(Globals.EXCEPTION_KEY);
        if (e2 != null) {
            StringWriter sw = new StringWriter();
            e2.printStackTrace(new PrintWriter(sw));
            String x = sw.getBuffer().toString();
            x = x.replaceAll("at ", "<br>at ");

            session.setAttribute(Token.MSG_ERROR, x);
        }

        return mapping.findForward(Token.SUCCESS);
    //ActionForward af = new ActionForward ("pages/error.jsp", true);
    //return af;	
    }
}